﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Admin_FriendLinkEdit : System.Web.UI.Page
{
    protected void Bind()
    {
        string str = base.Request.QueryString["FriendLinkId"];
        if (str == null)
        {
            this.ShowFriendLinkId.Text = "→ <a href=\"FriendLinkList.aspx\">请选择需编辑的链接</a>";
            this.Msg.Text = "→ <a href=\"FriendLinkList.aspx\">请选择需编辑的链接</a>";
            this.Msg.CssClass = "errorMsg";
            this.Msg.Visible = true;
            this.Edit.Enabled = false;
        }
        else
        {
            IDb db = new DbFactory().DriverDb();
            db.Open();
            DataRow row = db.TopRow("Title,FriendLinkId,Type,friendLinkDescribe,Url,LogoPath", "friendLink", string.Format("friendLinkId={0}", str), "");
            db.Close();
            this.ShowFriendLinkId.Text = (string)row["FriendLinkId"];
            this.FriendLinkTitle.Text = (string)row["Title"];
            this.TypeList.SelectedValue = ((string)row["type"]).ToLower();
            this.Url.Text = (string)row["Url"];
            this.friendLinkDescribe.Text = (string)row["friendLinkDescribe"];
            this.LogoPath.NavigateUrl = (string)row["LogoPath"];
            for (int i = 0; i < this.TypeList.Items.Count; i++)
            {
                this.TypeList.Items[i].Attributes.Add("onclick", "radioFriendLink(this)");
            }
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if ((this.TypeList.SelectedValue.ToLower() == "false") & (this.FriendLinkTitle.Text == ""))
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }

    protected void Edit_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            string navigateUrl = this.LogoPath.NavigateUrl;
            if (this.Logo.HasFile)
            {
                navigateUrl = string.Format("{0}FriendLink/{1}", FSO.GetUploadPath(), FSO.RandomFileName(this.Logo.FileName));
                string filePath = base.Server.MapPath(navigateUrl);
                FSO.PathExists(filePath);
                this.Logo.SaveAs(filePath);
            }
            IDb db = new DbFactory().DriverDb();
            db.Open();
            string[,] includeColumns = new string[,] { { "Title", this.FriendLinkTitle.Text, "NVarChar", "32" }, { "Type", this.TypeList.SelectedValue, "bit", "0" }, { "Url", this.Url.Text, "NVarChar", "100" }, { "friendLinkDescribe", this.friendLinkDescribe.Text, "NVarChar", "100" }, { "LogoPath", navigateUrl, "NVarChar", "100" }, { "SiteLanguage", SiteInfo.SiteLanguage(), "int", "0" } };
            db.Update("FriendLink", includeColumns, string.Format("FriendLinkId ={0}", this.ShowFriendLinkId.Text));
            db.Close();
            this.Msg.Text = "链接更新成功！";
            this.Msg.Visible = true;
            this.Bind();
        }
    }

    protected void LogoCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        if (this.Logo.HasFile)
        {
            string extension = FSO.GetExtension(this.Logo.FileName);
            string extensionList = ".jpg|.gif|.png|.bmp";
            args.IsValid = FSO.ExtensionValid(extension, extensionList);
            this.LogoCustomValidator.ErrorMessage = "Logo格式错误，合法格式：" + extensionList;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ManagerLogin.ValidateRole("C_FriendLinkEdit.aspx"))
        {
            this.Msg.CssClass = "errorMsg";
            this.Msg.Text = "您无此权限！";
            this.Msg.Visible = true;
            this.Edit.Enabled = false;
        }
        else
        {
            this.Msg.Visible = false;
            if (!base.IsPostBack)
            {
                this.Bind();
            }
        }
    }
}
